import { defineStore } from "pinia";
import { asyncRoutes } from "@/router/index.js";
// import { ref } from "vue";
function filterRoutes(routes, roles) {
  // 最终返回结果
  let result = [];
  routes.forEach((route) => {
    // 拷贝一份对象
    const tmp = { ...route };
    if (roles.includes(tmp.name)) {
      // 是否有 children
      if (tmp.children) {
        tmp.children = filterRoutes(tmp.children, roles);
      }

      result.push(tmp);
    }
  });

  return result;
}
export const useAsyncRoutesStore = defineStore("asyncRoutes", {
  state: () => {
    return {
      role: "",
      id: Number,
      username: "",
      permissionRoute: [],
      asyncRoutes,
    };
  },
  getters: {},
  actions: {
    getPermissionRoute() {
      filterRoutes(this.asyncRoutes, this.permissionRoute);
    },
    setStates(data) {
      this.role = data.role;
      this.id = data.id;
      this.username = data.username;
      this.permissionRoute = data.permissionRouteNames;
    },
  },
});
